//
//  AllKindViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/24.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "AllKindViewController.h"
#import "ChooselabCell.h"
#import "AllBtnCell.h"
#import "JiuDianViewController.h"
@interface AllKindViewController()<UITableViewDelegate,UITableViewDataSource,AllBtnCellDelegate>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation AllKindViewController

-(void)viewDidLoad
{
    
    [super viewDidLoad];
    
    _array = [NSMutableArray new];
    
    [self initTitleWithTitle:@"全部分类"];
    
    [self.view addSubview:self.table];
    
    [self getAllKind];
}

-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENW,SCREENH-64) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.showsHorizontalScrollIndicator = NO;
        _table.showsVerticalScrollIndicator = NO;
        _table.separatorStyle = 0;
        _table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _table.backgroundColor = [UIColor whiteColor];
    }
    return _table;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40*screenW_6;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [AllBtnCell cellHeight:_array[indexPath.section] Section:indexPath.section];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //加载Cell
    AllBtnCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"AllBtnCell"]];
    if (cell == nil) {
        cell = [[AllBtnCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"AllBtnCell"]];
    }
    cell.delegate = self;
    [cell loadDic:_array[indexPath.section] Section:indexPath.section];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //区头类别展示
    UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 40*screenW_6)];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15*screenW_6, 10*screenW_6, 20*screenW_6, 20*screenW_6)];
    NSString *imgUrl = [_array[section] objectForKey:@"image"];
    UIImage *newImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgUrl];//用地址去本地找图片
    if (newImage != nil) {//如果本地有
        // newImage就是图片
        imageView.image = newImage;
    }else{
        //下载图片
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:imgUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            //这里是处理下载进度的,好像没必要管他
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (image) {//下载完成后
                //同上处理
                imageView.image = image;
            }
        }];
    }
    [headerV addSubview:imageView];
    
    UILabel *titleLab = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(imageView.frame) + 10*screenW_6, 0, SCREENW - CGRectGetMaxX(imageView.frame), 40*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:TextBlack text:[_array[section] objectForKey:@"name"]];
    [headerV addSubview:titleLab];
    return headerV;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}

- (void)button:(NSInteger)num Section:(NSInteger)section
{
//    NSLog(@"num:%ld  section:%ld",(long)num,(long)section);
    JiuDianViewController *vc = [JiuDianViewController new];
    vc.typeDic = [[_array[section] objectForKey:@"secondList"] objectAtIndex:num];
    vc.cityName = _cityName;
    vc.cityID = _cityID;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getAllKind
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/other/findAllClassification" Dic:@{} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
//                NSLog(@"responseDic:%@",responseDic);
                if ([responseDic[@"success"] boolValue]) {
                    NSMutableArray *arrayData = [NSMutableArray new];
                    arrayData = [responseDic objectForKey:@"data"];
                    for (int i = 0; i<arrayData.count; i++) {

                        if ([[arrayData[i] objectForKey:@"secondList"] count] == 0) {
            
                        }else{
                            [_array addObject:arrayData[i]];
                        }
                    }
                    [_table reloadData];
                }
            });
        }];
    });
}

@end
